package mn.idax.exchange.otc.config;

import mn.idax.exchange.otc.enums.response.SystemStatusResponseEnum;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

//注解开启 swagger2 功能
@EnableSwagger2
@Configuration
public class Swagger2 {

    @Bean
    public Docket createRestApi() {

        List pars = getParams();
        List<ResponseMessage> responseMessages = getResponseMessages();

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
                .select()
                .apis(RequestHandlerSelectors.basePackage("mn.idax.exchange.otc"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .globalResponseMessage(RequestMethod.GET, responseMessages)
                .globalResponseMessage(RequestMethod.POST, responseMessages);
    }

    /**
     *
     * 功能描述:
     *      公共请求参数
     * @return: ArrayList<ParameterBuilder> 请求参数列表
     * @auther: dison
     * @date: 2018/8/9 14:16
     */
    private ArrayList<ParameterBuilder> getParams(){

        ArrayList pars = new ArrayList();
        pars.add(new ParameterBuilder().name("token").description("令牌").modelRef(new ModelRef("String")).parameterType("header").required(false).build());
        pars.add(new ParameterBuilder().name("Accept-Language").description("国际化标识(如：中文-zh_CN)").modelRef(new ModelRef("String")).parameterType("header").required(false).build());
        pars.add(new ParameterBuilder().name("usersource").description("请求来源(PC,IOS,ANDROID,H5)").modelRef(new ModelRef("String")).parameterType("header").required(false).build());
        return pars;
    }

    /**
     *
     * 功能描述:
     *      公共响应
     * @return: List<ResponseMessage> 响应列表信息
     * @auther: dison
     * @date: 2018/8/9 14:13
     */
    private List<ResponseMessage> getResponseMessages(){
        ArrayList<ResponseMessage> responseMessages = new ArrayList<ResponseMessage>();

        responseMessages.add(new ResponseMessageBuilder().code(SystemStatusResponseEnum.SUCCESS.getCode()).message("成功").build());
        responseMessages.add(new ResponseMessageBuilder().code(SystemStatusResponseEnum.FAIL.getCode()).message("失败").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(SystemStatusResponseEnum.SYSTEM_ERROR.getCode()).message("系统异常").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(400).message("请求参数错误").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(401).message("权限认证失败").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(403).message("请求资源不可用").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(404).message("请求资源不存在").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(409).message("请求资源冲突").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(415).message("请求格式错误").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(423).message("请求资源被锁定").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(500).message("服务器内部错误").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(501).message("请求方法不存在").responseModel(new ModelRef("Error")).build());
        responseMessages.add(new ResponseMessageBuilder().code(503).message("服务暂时不可用").responseModel(new ModelRef("Error")).build());

        return responseMessages;
    }

    //构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("mn.idax.exchange.otc 测试使用 Swagger2 构建RESTful API")
                //创建人
                .contact("沈迪雄")
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}
